# -*- coding: utf-8 -*-
#from aip import AipSpeech
from pygame import mixer
import time
import os
import random

#百度后台获取的秘钥
APP_ID = '23054193'
API_KEY = 'Q5IpmtjQGnRLeDY5dKjCtrX9'
SECRET_KEY = 'w67avBAWRymwvISugiuvTG4z1HlqVmzg'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


def play(text):
    result = client.synthesis( text,'zh',1,{'vol':5,})
    cap = str(random.randint(100001,999999))
    file_name = cap + '.mp3' #文件的名字
    if not isinstance(result, dict):
        with open( file_name, 'wb') as f:
            f.write(result)
    return file_name

def paly_mp3(file_name):
    mixer.init()
    mixer.music.load(file_name)
    mixer.music.play()
    time.sleep(10)
    mixer.music.stop()

def allplay(text):
    file_name = play(text)
    paly_mp3(file_name)


# text =  '你是爱，是暖，是希望！你是人间四月天！'
# allplay(text)

